import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:jiffy/jiffy.dart';
import 'package:safetymonitor/app/components/custom_app_bar.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_file_upload_page.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_high_input_page.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_input_page.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_picker_photo.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_picker_video.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_select_page.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_sure_sheet.dart';
import 'package:safetymonitor/app/core/core.dart';

import 'controller.dart';

/// 新增隐患
class AddHazardPage extends StatelessWidget {
  AddHazardPage({super.key});

  final controller = Get.put(AddHazardController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Scaffold(
            backgroundColor: const Color(0xFFF7FBFE),
            appBar: CustomAppBar(
              titleText: '新增隐患',
            ),
            body: SingleChildScrollView(
              child: Column(
                children: [
                  AppGaps.vGap20,
                  _getBasisContentView(),
                  // _getOtherContentView(),
                  AppGaps.vGap60,
                ],
              ),
            ),
            bottomSheet: CustomSureSheet(onTap: controller.confirm),
          );
        });
  }

  /// 选择框
  Widget _getChoiceView(String title, String value, {String? hint}) {
    return Column(
      children: [
        Row(
          children: [
            Text(
              title,
              style: TextStyle(
                  fontSize: 14.sp,
                  height: 1.2,
                  color: const Color(0xFF262F38),
                  fontWeight: FontWeight.w400),
            ),
            AppGaps.hGap50,
            Expanded(
              child: Container(
                alignment: Alignment.centerRight,
                child: Text(
                  value == '' ? '请选择${hint ?? title}' : value,
                  style: TextStyle(
                    fontSize: 14.sp,
                    height: 1.2,
                    color: value == ''
                        ? const Color(0xFFA0A7B5)
                        : const Color(0xFF666666), //const Color(0xFF262F38),
                  ),
                ),
              ),
            ),
            AppGaps.hGap6,
            Icon(Icons.arrow_forward_ios,
                size: 20.w, color: const Color(0xFF6C779C))
          ],
        )
      ],
    );
  }

  /// 填写输入内容
  Widget _getBasisContentView() {
    return Container(
      padding: EdgeInsets.all(20.w),
      margin: EdgeInsets.symmetric(horizontal: 20.w),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(10.w),
      ),
      child: Column(
        children: [
          CustomInputPage(
            title: '隐患名称：',
            text: controller.preventiveRectificationCreateEntity.preventiveName,
            onCallBack: (value) {
              controller.preventiveRectificationCreateEntity.preventiveName =
                  value;
            },
          ),
          //👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇 二选一显示
          Visibility(
            visible:  controller.preventiveRectificationCreateEntity.isHead,
            child: CustomSelectPage(
              selectText: controller.preventiveRectificationCreateEntity.companyName,
              title: '责任单位：',
              onCallBack: () {
                controller.selectCurrentDept();
                controller.update();
              },
            ),
          ),
          // 选择完责任单位后,选择责任部门
          Visibility(
            visible:  controller.preventiveRectificationCreateEntity.isHead,
            child: CustomSelectPage(
              selectText: controller.selectIdDeptName,
              title: '责任部门：',
              onCallBack: () {
                controller.selectCurrentDept2();
                controller.update();
              },
            ),
          ),
          // 选完责任部门后,根据选择的部门去拿责任人
          Visibility(
            visible: controller.preventiveRectificationCreateEntity.isHead,
            child: CustomSelectPage(
              selectText: controller.selectIdPersonName,
              title: '责任人：',
              onCallBack: () {
                controller.selectCurrentDept3();
                controller.update();
              },
            ),
          ),

          // 整改部门
          Visibility(
            visible: !controller.preventiveRectificationCreateEntity.isHead,
            child: CustomSelectPage(
              selectText: controller.selectIdRectificationName,
              title: '整改部门：',
              onCallBack: () {
                controller.selectRectificationDept();
              },
            ),
          ),
          Visibility(
            visible: !controller.preventiveRectificationCreateEntity.isHead,
            child: CustomSelectPage(
              selectText:   controller.selectIdRectificationPersonName,
              title: '整改人：',
              onCallBack: () {
                controller.selectRectificationPerson();
              },
            ),
          ),
          //👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆 二选一显示
          CustomSelectPage(
              selectText:
              controller.preventiveRectificationCreateEntity.findTime==0?'':  Jiffy.parseFromMillisecondsSinceEpoch( controller.preventiveRectificationCreateEntity.findTime).format(pattern: 'yyyy-MM-dd HH:mm:ss'),
              title: '发现时间：',
              onCallBack: () {
                controller.showDatePicker();
              }),
          CustomSelectPage(
              selectText: controller.preventiveRectificationCreateEntity.rectificationDeadline==0?'': Jiffy.parseFromMillisecondsSinceEpoch( controller.preventiveRectificationCreateEntity.rectificationDeadline).format(pattern: 'yyyy-MM-dd HH:mm:ss'),
              title: '整改期限：',
              onCallBack: () {
                controller.showDatePicker2();
              }),
          CustomHighInputPage(
              isRequired: false,
              title: '隐患来源：',
              text: controller.preventiveRectificationCreateEntity.source,
              hintText: '请输入来源',
              onCallBack: (value) {
                controller.preventiveRectificationCreateEntity.source = value;
              }),
          CustomHighInputPage(
              title: '问题描述：',
              text: controller.preventiveRectificationCreateEntity.problemDesc,
              hintText: '请输入描述',
              onCallBack: (value) {
                controller.preventiveRectificationCreateEntity.problemDesc =
                    value;
              }),
          CustomHighInputPage(
              isRequired: false,
              title: '整改要求：',
              text: controller
                  .preventiveRectificationCreateEntity.rectificationRequire,
              hintText: '请输入要求',
              onCallBack: (value) {
                controller.preventiveRectificationCreateEntity
                    .rectificationRequire = value;
              }),
          AppGaps.vGap20,
          CustomFileUploadPage(
            isRequired: false,
            isShowDivider: false,
            splitCommaInUrl: true,
            // uploadedFiles: [],
            title: '附件',
            onCallBack: (value) {
              final String imgUrl = value.join(',');
              controller.preventiveRectificationCreateEntity.fileUrl = imgUrl;
            },
          ),
        ],
      ),
    );
  }

  /// 获取其他信息
  Widget _getOtherContentView() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 20.w),
      margin: EdgeInsets.symmetric(horizontal: 20.w),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(10.w),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          CustomPickerPhoto(
            title: '照片:',
            maxCount: 9,
            imagesList: controller.dangerImages,
          ),
          Divider(height: 0.5.w, color: const Color(0xffEAF2FA)),
          CustomPickerVideo(
            title: '视频:',
            maxCount: 1,
            videoList: controller.dangerVideo,
          ),
        ],
      ),
    );
  }
}
